using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace JGLib.GUI
{
    public class Control
    {
        public int X { get; set; }
        public int Y { get; set; }

        public int Width { get; set; }
        public int Height { get; set; }

        public Texture2D Background { get; set; }
        public Texture2D Foreground { get; set; }

        //protected void DrawResizeableTex(SpriteBatch g, GraphicsDevice device, int width, int height,
        //    Texture2D topLeft, Texture2D topRight, Texture2D bottomLeft, Texture2D bottomRight,
        //    Texture2D top, Texture2D right, Texture2D left, Texture2D bottom, Texture2D center)
        //{
        //    Color[] centerTex = null;
        //    center.GetData<Color>(centerTex);
        //    Color centerColor = centerTex[0];
        //    DrawResizeableTex(g, device, width, height, topLeft, topRight, bottomLeft, bottomRight,
        //        top, right, left, bottom, (x, y) => centerColor);
        //}
        //protected delegate Color TextureFromCoord(int x, int y);
        //protected void DrawResizeableTex(SpriteBatch g, GraphicsDevice device, int width, int height,
        //    Texture2D topLeft, Texture2D topRight, Texture2D bottomLeft, Texture2D bottomRight,
        //    Texture2D top, Texture2D right, Texture2D left, Texture2D bottom, TextureFromCoord center)
        //{
        //    Color[] centerData = new Color[width * height];
        //    for (int x = 0; x < width; x++)
        //        for (int y = 0; y < height; y++)
        //            centerData[x + y * width] = center(x, y);

        //    Texture2D centerTex = new Texture2D(device, width, height);
        //    centerTex.SetData<Color>(centerData);

        //    g.Draw(centerTex, new Vector2(0, 0), Color.White); // Background
        //    g.Draw(topLeft, new Vector2(0, 0), Color.White); // Top-Left
        //    g.Draw(topRight, new Vector2(width - 1 - topRight.Width, 0), Color.White); // Top-Right
        //    g.Draw(bottomLeft, new Vector2(0, height - 1 - bottomLeft.Height), Color.White); // Bottom-Left
        //    g.Draw(bottomRight, new Vector2(width - 1 - bottomRight.Width, height - 1 - bottomRight.Height), Color.White); // Bottom-Right

        //    for (int x = topLeft.Width; x < width - topRight.Width; x++)
        //        g.Draw(top, new Vector2(x, 0), Color.White); // Top
        //    for (int x = bottomLeft.Width; x < width - bottomRight.Width; x++)
        //        g.Draw(bottom, new Vector2(x, height - 1 - bottom.Height), Color.White); // Bottom
        //    for (int y = topLeft.Height; y < height - bottomLeft.Height; y++)
        //        g.Draw(left, new Vector2(0, y), Color.White); // Left
        //    for (int y = topRight.Height; y < height - bottomRight.Height; y++)
        //        g.Draw(right, new Vector2(width - 1 - right.Width, y), Color.White); // Right
        //}
        protected void Draw9Patch(SpriteBatch g, Texture2D tex, int width, int height)
        {
            Color[] texData = null;
            tex.GetData<Color>(texData);

            int stretchTopStart = -1, stretchTopEnd = -1,
                stretchLeftStart = -1, stretchLeftEnd = -1;
            for (int x = 0; x < tex.Width; x++)
            {
                Color c = GetTexColor(texData, x, 0, tex.Width);
                if (c.A > 0 && stretchTopStart == -1)
                    stretchTopStart = x;
                if (c.A == 0 && stretchTopStart != -1)
                {
                    stretchTopEnd = x - 1;
                    break;
                }
            }
            for (int y = 0; y < tex.Height; y++)
            {
                Color c = GetTexColor(texData, 0, y, tex.Width);
                if (c.A > 0 && stretchTopStart == -1)
                    stretchLeftStart = y;
                if (c.A == 0 && stretchTopStart != -1)
                {
                    stretchLeftEnd = y - 1;
                    break;
                }
            }


        }
        protected Color GetTexColor(Color[] texture, int x, int y, int width)
        {
            return texture[x + y * width];
        }
    }
}
